$OpenBSD: patch-src_dvdbackup_c,v 1.3 2009/07/30 19:10:28 jakemsr Exp $
--- src/dvdbackup.c.orig	Sun Aug  4 23:08:39 2002
+++ src/dvdbackup.c	Mon Jul 13 16:58:47 2009
@@ -30,6 +30,7 @@
 #include <unistd.h>
 #include <string.h>
 #include <limits.h>
+#include <stdint.h>
 #include <dvdread/dvd_reader.h>
 #include <dvdread/ifo_read.h>
 #include <dvdread/ifo_print.h>
@@ -227,7 +227,8 @@ void usage(){
 }
 
 int CheckSizeArray(const int size_array[], int reference, int target) {
-	if ( (size_array[reference]/size_array[target] == 1) &&
+	if ( (size_array[target] != 0) &&
+	     (size_array[reference]/size_array[target] == 1) &&
 	     ((size_array[reference] * 2 - size_array[target])/ size_array[target] == 1) &&
 	     ((size_array[reference]%size_array[target] * 3) < size_array[reference]) ) {
 		/* We have a dual DVD with two feature films - now lets see if they have the same amount of chapters*/
@@ -301,7 +302,7 @@ int DVDWriteCells(dvd_reader_t * dvd, int cell_start_s
 
 
 	/* Vob control */
-	int vob;
+	int vob = 0;
 
 	/* Temp filename,dirname */
 	char targetname[PATH_MAX];
@@ -313,9 +314,9 @@ int DVDWriteCells(dvd_reader_t * dvd, int cell_start_s
 	/* File Handler */
 	int streamout;
 
-	int size;
-	int left;
-	int leftover;
+	int size = 0;
+	int left = 0;
+	int leftover = 0;
 
 	/* Buffer size in DVD sectors */
 	/* Currently set to 1MB */
@@ -353,7 +354,7 @@ int DVDWriteCells(dvd_reader_t * dvd, int cell_start_s
 	/* Remove all old files silently if they exists */
 
 	for ( i = 0 ; i < 10 ; i++ ) {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, i + 1);
+		snprintf(targetname, sizeof(targetname),"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, i + 1);
 #ifdef DEBUG
 		fprintf(stderr,"DVDWriteCells: file is %s\n", targetname);
 
@@ -423,7 +424,7 @@ int DVDWriteCells(dvd_reader_t * dvd, int cell_start_s
 			fprintf(stderr,"Don't try to copy chapters from the VMG domain there aren't any\n");
 			return(1);
 		} else {
-			sprintf(targetname,"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, vob);
+			snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, vob);
 		}
 
 #ifdef DEBUG
@@ -504,7 +505,7 @@ int DVDWriteCells(dvd_reader_t * dvd, int cell_start_s
 				fprintf(stderr,"Don't try to copy chapters from the VMG domain there aren't any\n");
 				return(1);
 			} else {
-				sprintf(targetname,"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, vob);
+				snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, vob);
 			}
 
 
@@ -602,7 +603,7 @@ titles_info_t * DVDGetInfo(dvd_reader_t * _dvd) {
 	int counter, i, f;
 
 	/* Our guess */
-	int candidate;
+	int candidate = 0;
 	int multi = 0;
 	int dual = 0;
 
@@ -1083,7 +1084,7 @@ int DVDCopyTileVobX(dvd_reader_t * dvd, title_set_info
 		fprintf(stderr,"Don't try to copy a Title VOB from the VMG domain there aren't any\n");
 		return(1);
 	} else {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, vob);
+		snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VTS_%02i_%i.VOB",targetdir, title_name, title_set, vob);
 	}
 
 
@@ -1217,9 +1218,9 @@ int DVDCopyMenu(dvd_reader_t * dvd, title_set_info_t *
 
 	/* Create VIDEO_TS.VOB or VTS_XX_0.VOB */
 	if (title_set == 0) {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VIDEO_TS.VOB",targetdir, title_name);
+		snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VIDEO_TS.VOB",targetdir, title_name);
 	} else {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VTS_%02i_0.VOB",targetdir, title_name, title_set);
+		snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VTS_%02i_0.VOB",targetdir, title_name, title_set);
 	}
 
 
@@ -1330,9 +1331,9 @@ int DVDCopyIfoBup (dvd_reader_t * dvd, title_set_info_
 	/* Create VIDEO_TS.IFO or VTS_XX_0.IFO */
 
 	if (title_set == 0) {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VIDEO_TS.IFO",targetdir, title_name);
+		snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VIDEO_TS.IFO",targetdir, title_name);
 	} else {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VTS_%02i_0.IFO",targetdir, title_name, title_set);
+		snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VTS_%02i_0.IFO",targetdir, title_name, title_set);
 	}
 
 	if (stat(targetname, &fileinfo) == 0) {
@@ -1396,9 +1397,9 @@ int DVDCopyIfoBup (dvd_reader_t * dvd, title_set_info_
 	/* Create VIDEO_TS.BUP or VTS_XX_0.BUP */
 
 	if (title_set == 0) {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VIDEO_TS.BUP",targetdir, title_name);
+		snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VIDEO_TS.BUP",targetdir, title_name);
 	} else {
-		sprintf(targetname,"%s/%s/VIDEO_TS/VTS_%02i_0.BUP",targetdir, title_name, title_set);
+		snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS/VTS_%02i_0.BUP",targetdir, title_name, title_set);
 	}
 
 
@@ -1518,7 +1519,7 @@ int DVDMirrorTitleX(dvd_reader_t * dvd, title_set_info
 int DVDGetTitleName(const char *device, char *title)
 {
 	/* Variables for filehandel and title string interaction */
-
+	char buf[2048];
 	int  filehandle, i, last;
 
 	/* Open DVD device */
@@ -1530,7 +1531,7 @@ int DVDGetTitleName(const char *device, char *title)
 
 	/* Seek to title of first track, which is at (track_no * 32768) + 40 */
 
-	if ( 32808 != lseek(filehandle, 32808, SEEK_SET) ) {
+	if ( 32768 != lseek(filehandle, 32768, SEEK_SET) ) {
 		close(filehandle);
 		fprintf(stderr, "Can't seek DVD device %s - check your DVD device\n", device);
 		return(1);
@@ -1538,21 +1539,20 @@ int DVDGetTitleName(const char *device, char *title)
 
 	/* Read the DVD-Video title */
 
-	if ( 32 != read(filehandle, title, 32)) {
+	if (read(filehandle, buf, sizeof(buf)) == -1) {
 		close(filehandle);
 		fprintf(stderr, "Can't read title from DVD device %s\n", device);
 		return(1);
 	}
 
 	/* Terminate the title string */
+	strlcpy(title, buf + 40, 32);
 
-	title[32] = '\0';
-
-
 	/* Remove trailing white space */
-
-	last = 32;
+	last = 31;
 	for ( i = 0; i < 32; i++ ) {
+		if (title[i] == '\0')
+			break;
 		if ( title[i] != ' ' ) { last = i; }
 	}
 
@@ -1687,7 +1687,7 @@ title_set_info_t *DVDGetFileSet(dvd_reader_t * _dvd) {
 
 	/* Find VIDEO_TS.IFO is present - must be present since we did a ifo open 0*/
 
-	sprintf(filename,"/VIDEO_TS/VIDEO_TS.IFO");
+	snprintf(filename,sizeof(filename),"/VIDEO_TS/VIDEO_TS.IFO");
 
 	if ( UDFFindFile(_dvd, filename, &size) != 0 ) {
 		title_set_info->title_set[0].size_ifo = size;
@@ -1700,7 +1700,7 @@ title_set_info_t *DVDGetFileSet(dvd_reader_t * _dvd) {
 
 	/* Find VIDEO_TS.VOB if present*/
 
-	sprintf(filename,"/VIDEO_TS/VIDEO_TS.VOB");
+	snprintf(filename,sizeof(filename),"/VIDEO_TS/VIDEO_TS.VOB");
 
 	if ( UDFFindFile(_dvd, filename, &size) != 0 ) {
 		title_set_info->title_set[0].size_menu = size;
@@ -1710,7 +1710,7 @@ title_set_info_t *DVDGetFileSet(dvd_reader_t * _dvd) {
 
 	/* Find VIDEO_TS.BUP if present */
 
-	sprintf(filename,"/VIDEO_TS/VIDEO_TS.BUP");
+	snprintf(filename,sizeof(filename),"/VIDEO_TS/VIDEO_TS.BUP");
 
 	if ( UDFFindFile(_dvd, filename, &size) != 0 ) {
 		title_set_info->title_set[0].size_bup = size;
@@ -1745,7 +1745,7 @@ title_set_info_t *DVDGetFileSet(dvd_reader_t * _dvd) {
 			}
 
 
-			sprintf(filename,"/VIDEO_TS/VTS_%02i_0.IFO",counter + 1);
+			snprintf(filename,sizeof(filename),"/VIDEO_TS/VTS_%02i_0.IFO",counter + 1);
 
 			if ( UDFFindFile(_dvd, filename, &size) != 0 ) {
 				title_set_info->title_set[counter + 1].size_ifo = size;
@@ -1761,7 +1761,7 @@ title_set_info_t *DVDGetFileSet(dvd_reader_t * _dvd) {
 
 			/* Find VTS_XX_0.VOB if present*/
 
-			sprintf(filename,"/VIDEO_TS/VTS_%02i_0.VOB", counter + 1);
+			snprintf(filename,sizeof(filename),"/VIDEO_TS/VTS_%02i_0.VOB", counter + 1);
 
 			if ( UDFFindFile(_dvd, filename, &size) != 0 ) {
 				title_set_info->title_set[counter + 1].size_menu = size;
@@ -1778,7 +1778,7 @@ title_set_info_t *DVDGetFileSet(dvd_reader_t * _dvd) {
 			/* Find all VTS_XX_[1 to 9].VOB files if they are present*/
 
 			for( i = 0; i < 9; ++i ) {
-				sprintf(filename,"/VIDEO_TS/VTS_%02i_%i.VOB", counter + 1, i + 1 );
+				snprintf(filename,sizeof(filename),"/VIDEO_TS/VTS_%02i_%i.VOB", counter + 1, i + 1 );
 				if(UDFFindFile(_dvd, filename, &size) == 0 ) {
 					break;
 				}
@@ -1791,7 +1791,7 @@ title_set_info_t *DVDGetFileSet(dvd_reader_t * _dvd) {
 			}
 
 
-			sprintf(filename,"/VIDEO_TS/VTS_%02i_0.BUP", counter + 1);
+			snprintf(filename,sizeof(filename),"/VIDEO_TS/VTS_%02i_0.BUP", counter + 1);
 
 			if ( UDFFindFile(_dvd, filename, &size) != 0 ) {
 				title_set_info->title_set[counter +1].size_bup = size;
@@ -2265,9 +2265,9 @@ int main(int argc, char *argv[]){
 
 	/* Switches */
 	int title_set = 0;
-	int titles;
-	int start_chapter;
-	int end_chapter;
+	int titles = 0;
+	int start_chapter = 0;
+	int end_chapter = 0;
 
 	int do_mirror = 0;
 	int do_title_set = 0;
@@ -2278,7 +2278,7 @@ int main(int argc, char *argv[]){
 
 
 
-	int return_code;
+	int return_code = 0;
 
 	/* DVD Video device */
 	char * dvd=NULL;
@@ -2499,7 +2499,7 @@ int main(int argc, char *argv[]){
 
 
 
-	sprintf(targetname,"%s",targetdir);
+	snprintf(targetname,sizeof(targetname),"%s",targetdir);
 
 	if (stat(targetname, &fileinfo) == 0) {
 		if (! S_ISDIR(fileinfo.st_mode)) {
@@ -2515,7 +2515,7 @@ int main(int argc, char *argv[]){
 	}
 
 
-	sprintf(targetname,"%s/%s",targetdir, title_name);
+	snprintf(targetname,sizeof(targetname),"%s/%s",targetdir, title_name);
 
 	if (stat(targetname, &fileinfo) == 0) {
 		if (! S_ISDIR(fileinfo.st_mode)) {
@@ -2530,7 +2530,7 @@ int main(int argc, char *argv[]){
 		}
 	}
 
-	sprintf(targetname,"%s/%s/VIDEO_TS",targetdir, title_name);
+	snprintf(targetname,sizeof(targetname),"%s/%s/VIDEO_TS",targetdir, title_name);
 
 	if (stat(targetname, &fileinfo) == 0) {
 		if (! S_ISDIR(fileinfo.st_mode)) {
